package ioc

import (
	rlock "github.com/gotomicro/redis-lock"
	"github.com/robfig/cron/v3"
	"jk-time/webook/internal/service"
	job2 "jk-time/webook/internal/web/job"
	"jk-time/webook/pkg/jobx"
	"time"
)

func InitRankingJob(svc service.RankingService, lockClient *rlock.Client) *job2.RankingJob {
	return job2.NewRankingJob(svc, time.Second, lockClient)
}
func InitJobs(rankingJob *job2.RankingJob) *cron.Cron {
	cronN := cron.New(cron.WithSeconds())

	// 依靠适配器实现rankingJob变成job
	jobN := jobx.NewCronJobBuilder("ranking_job", "redis", "redis_ranking_job",
		"执行redis_ranking循环任务").Build(rankingJob)
	_, err := cronN.AddJob("* */3 * * * ?", jobN)
	if err != nil {
		panic(err)
	}
	// 先查一波，不等太久
	jobN.Run()
	return cronN
}

// InitJobs(NewCronJobBuilder观测->Build(实现job接口))->InitRankingJob->NewRankingJob->RankingJob(Run)
